# 覆冰监测控制器Makefile - 适配RK3588平台
# 默认使用系统编译器，RK3588上不需要交叉编译

# 编译器设置
CC = gcc
CXX = g++

# 检查OpenCV是否存在
OPENCV_EXISTS := $(shell pkg-config --exists opencv4 && echo "yes" || echo "no")

# 编译标志
CFLAGS = -Wall -O2 -std=c99 -D_GNU_SOURCE -Wno-unused-function
CXXFLAGS = -Wall -O2 -std=c++11 -D_GNU_SOURCE -Wno-unused-function
# 如果找不到系统头文件，添加标准路径
ifneq ($(OS),Windows_NT)
    CFLAGS += -I/usr/include -I/usr/include/aarch64-linux-gnu -I/rk3588/usr/include
    CXXFLAGS += -I/usr/include -I/usr/include/aarch64-linux-gnu -I/rk3588/usr/include
else
    # Windows环境下使用MinGW标准路径
    CFLAGS += -I"C:/mingw64/include" -I"C:/mingw64/x86_64-w64-mingw32/include"
    CXXFLAGS += -I"C:/mingw64/include" -I"C:/mingw64/x86_64-w64-mingw32/include"
endif
LDFLAGS = -lpthread -lm -lrt -ldl

DEPFLAGS = -MMD -MP

# 添加FFmpeg依赖
FFMPEG_CFLAGS = $(shell pkg-config --cflags libavcodec libavformat libavutil libswscale 2>/dev/null || echo "-I/usr/include")
FFMPEG_LDFLAGS = $(shell pkg-config --libs libavcodec libavformat libavutil libswscale 2>/dev/null || echo "-lavcodec -lavformat -lavutil -lswscale")

# 添加curl依赖
CURL_CFLAGS = $(shell pkg-config --cflags libcurl 2>/dev/null || echo "-I/usr/include")
CURL_LDFLAGS = $(shell pkg-config --libs libcurl 2>/dev/null || echo "-lcurl")

# 添加OpenCV依赖，仅当OpenCV存在时
ifeq ($(OPENCV_EXISTS),yes)
    OPENCV_CFLAGS = $(shell pkg-config --cflags opencv4)
    OPENCV_LDFLAGS = $(shell pkg-config --libs opencv4)
    # 启用OpenCV支持
    CFLAGS += -DHAVE_OPENCV
    CXXFLAGS += -DHAVE_OPENCV
    # 添加C++源文件
    CXX_SRC = opencv_wrapper.cpp
else
    # 禁用OpenCV支持
    OPENCV_CFLAGS =
    OPENCV_LDFLAGS =
    # 通知使用模拟图像处理
    CFLAGS += -DNO_OPENCV
    CXXFLAGS += -DNO_OPENCV
    # 没有C++源文件
    CXX_SRC =
endif

# 合并标志
CFLAGS += $(FFMPEG_CFLAGS) $(OPENCV_CFLAGS) $(CURL_CFLAGS)
CXXFLAGS += $(FFMPEG_CFLAGS) $(OPENCV_CFLAGS) $(CURL_CFLAGS)
LDFLAGS += $(FFMPEG_LDFLAGS) $(OPENCV_LDFLAGS) $(CURL_LDFLAGS)

# 调试和发布模式配置
ifdef DEBUG
    CFLAGS += -g -DDEBUG
    CXXFLAGS += -g -DDEBUG
    OUTDIR = debug
else
    CFLAGS += -O2 -DNDEBUG
    CXXFLAGS += -O2 -DNDEBUG
    OUTDIR = release
endif

# 源文件和头文件
# 已整合的文件：
# 1. common_filters.c/h -> 整合到image_process.c/h
# 2. monitor_common.c/h -> 整合到visual_monitor.c和stereo_monitor.c
# 3. global_mutex.c/h -> 整合到thread_utils.c/h
# 4. system_diagnostics.c/h -> 整合到system_utils.c/h
C_SRC = main.c app_control.c system_utils.c thread_utils.c \
        visual_monitor.c stereo_monitor.c uart_handler.c simulation.c \
        image_process.c data_fusion.c debug_images.c \
        ice_calculation.c camera_time_sync.c camera_utils.c camera_capture_mode.c \
        camera_capture_thread.c hybrid_mode_thread.c

# 生成目标和中间文件
C_OBJ = $(patsubst %.c,$(OUTDIR)/%.o,$(C_SRC))
CXX_OBJ = $(patsubst %.cpp,$(OUTDIR)/%.o,$(CXX_SRC))
OBJ = $(C_OBJ) $(CXX_OBJ)
DEPS = $(OBJ:.o=.d)

# 目标二进制文件
TARGET = $(OUTDIR)/ice_monitor

# 伪目标
.PHONY: all clean help debug install

# 默认目标
all: $(TARGET)

# 调试模式
debug:
	$(MAKE) DEBUG=1

# 创建输出目录
$(OUTDIR):
	mkdir -p $(OUTDIR)

# 编译.c文件到.o文件
$(OUTDIR)/%.o: %.c | $(OUTDIR)
	$(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@

# 编译.cpp文件到.o文件
$(OUTDIR)/%.o: %.cpp | $(OUTDIR)
	$(CXX) $(CXXFLAGS) $(DEPFLAGS) -c $< -o $@

# 链接目标文件
$(TARGET): $(OBJ)
	$(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS)
	@echo "编译完成: $@"

# 清理编译文件
clean:
	rm -rf $(OUTDIR)

# 安装到系统
install: $(TARGET)
	install -m 755 $(TARGET) /usr/local/bin/

# 帮助信息
help:
	@echo "覆冰监测控制器 Makefile 帮助"
	@echo "make       - 编译项目(默认是release模式)"
	@echo "make debug - 以调试模式编译"
	@echo "make clean - 清理编译文件"
	@echo "make install - 安装到系统"
	@echo "make help  - 显示此帮助信息"

# 包含依赖文件
-include $(DEPS)